package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.crm.service.impl.Gwai120ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 注册信息控制类
 *
 * @author yuan_rz
 * @Date 2019/01/15 14:54
 */
@Controller
@RequestMapping("/base/gwai120")
public class Gwai120Controller extends BaseController {

    @Autowired
    private Gwai120ServiceImpl gwai120Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwai120:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwai120.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwai120Service.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportListData", method = RequestMethod.GET)
    @RequiresPermissions("base:gwai120:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response) {

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwaaT gwaaT = new GwaaT();
        gwaaT.setGWAA002(inDto.getString("GWAA002"));
        gwaaT.setGWAA003(inDto.getString("GWAA003"));
        gwaaT.setGWAA004(inDto.getString("GWAA004"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        gwaaT.setGWAA005(LocalDate.parse(inDto.getString("GWAA005"), formatter));
        gwaaT.setGWAA006(inDto.getString("GWAA006"));
        gwaaT.setGWAA007(inDto.getString("GWAA007"));
        gwaaT.setGWAA008(inDto.getString("GWAA008"));
        gwaaT.setGWAA009(inDto.getString("GWAA009"));
        gwaaT.setGWAA010(inDto.getString("GWAA010"));
        gwaaT.setGWAA011(inDto.getString("GWAA011"));
        gwaaT.setGWAA012(inDto.getString("GWAA012"));
        gwaaT.setGWAA013(inDto.getString("GWAA013"));
        gwaaT.setGWAA014(inDto.getString("GWAA014"));
        gwaaT.setGWAA015(inDto.getString("GWAA015"));
        gwaaT.setGWAA016(inDto.getString("GWAA016"));
        gwaaT.setGWAA017(inDto.getString("GWAA017"));
        gwaaT.setGWAA018(inDto.getString("GWAA018"));
        gwaaT.setGWAA022(inDto.getString("GWAA022"));
        gwaaT.setGWAAACTI(inDto.getString("GWAAACTI"));
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaaT.setGWAAENT(shiroUser.getDBC());
        if (gwai120Service.existedTheParams(gwaaT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwai120Service.addData(gwaaT, inDto);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwaaT gwaaT = new GwaaT();
        gwaaT.setGWAAENT(inDto.getString("ENT"));
        gwaaT.setGWAA002(inDto.getString("GWAA002"));
        gwaaT.setGWAA003(inDto.getString("GWAA003"));
        gwaaT.setGWAA004(inDto.getString("GWAA004"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        gwaaT.setGWAA005(LocalDate.parse(inDto.getString("GWAA005"), formatter));
        gwaaT.setGWAA006(inDto.getString("GWAA006"));
        gwaaT.setGWAA007(inDto.getString("GWAA007"));
        gwaaT.setGWAA008(inDto.getString("GWAA008"));
        gwaaT.setGWAA009(inDto.getString("GWAA009"));
        gwaaT.setGWAA010(inDto.getString("GWAA010"));
        gwaaT.setGWAA011(inDto.getString("GWAA011"));
        gwaaT.setGWAA012(inDto.getString("GWAA012"));
        gwaaT.setGWAA013(inDto.getString("GWAA013"));
        gwaaT.setGWAA014(inDto.getString("GWAA014"));
        gwaaT.setGWAA015(inDto.getString("GWAA015"));
        gwaaT.setGWAA016(inDto.getString("GWAA016"));
        gwaaT.setGWAA017(inDto.getString("GWAA017"));
        gwaaT.setGWAA018(inDto.getString("GWAA018"));
        gwaaT.setGWAA022(inDto.getString("GWAA022"));
        gwaaT.setGWAAACTI(inDto.getString("GWAAACTI"));
        gwai120Service.editData(gwaaT,inDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData deleteData(GwaaT gwaaT) {
        gwai120Service.deleteData(gwaaT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwaaT> gwaaTList = inDto.getReqList(GwaaT.class);
        gwai120Service.deleteDataBatch(gwaaTList);
        return SUCCESS_TIP;
    }

    /**
     * 查询省市列表
     */
    @RequestMapping("/getProvinceCityList")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData getProvinceCityList(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        inDto.put("lang", "2");
        return new SuccessResponseData(gwai120Service.getProvinceCityList(inDto));
    }

    /**
     * 查询客户行业列表
     */
    @RequestMapping("/getKhhyList")
    @RequiresPermissions("base:gwai120:init")
    @ResponseBody
    public ResponseData getKhhyList(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        inDto.put("lang", "2");
        return new SuccessResponseData(gwai120Service.getKhhyList(inDto));
    }

    /**
     * 查询归属对象列表
     */
    @RequestMapping("/getGsdxList")
    @ResponseBody
    public ResponseData getGsdxList(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        inDto.put("lang", "2");
        return new PageReData(page,gwai120Service.getGsdxList(page,inDto));
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getRolesList")
    @ResponseBody
    public ResponseData getRolesList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwai120Service.getRolesList(page, inDto);
        return new PageReData(page, result);
    }
}
